<?php
namespace App\Http\Controllers\Admin\Job;

use App\Repositories\Admin\JobCompanyRepository;
use App\Utilities\JiugeForm;
use Illuminate\Http\Request;

class CompanyController extends BaseController
{
    /**
     * 企业职位
     */

    protected $view = 'admin.job.company.';//视图前缀
    protected $url = 'company';//路由前缀
    protected $model;

    public function __construct()
    {
        parent::__construct();
        $this->prefix_url = $this->prefix_url.'/'.$this->url;
        $this->model = new JobCompanyRepository();
        view()->share('prefix_url',$this->prefix_url);
        view()->share('jiugeForm',new JiugeForm($this->prefix_url));
    }

    public function index()
    {
        $models = $this->model->getAdminIndex();
        return view($this->view.'index',array(
            'datas' => $models,
        ));
    }

    public function create()
    {
        $welfares = $this->model->getWelfares();
        $rests = $this->model->getRests();
        $coms = $this->model->getCompanys();
        $edus = $this->model->getEdus();
        $workYears = $this->model->getWorkYears();
        return view($this->view.'edit',array(
            'welfares' => $welfares,
            'rests' => $rests,
            'coms' => $coms,
            'edus' => $edus,
            'workYears' => $workYears,
        ));
    }

    public function store(Request $request)
    {
        $resData = $this->getData($request);
        $lastId = $this->model->setAdd($resData);
        return redirect($this->prefix_url);
    }

    public function edit($id)
    {
        $welfares = $this->model->getWelfares();
        $rests = $this->model->getRests();
        $coms = $this->model->getCompanys();
        $edus = $this->model->getEdus();
        $workYears = $this->model->getWorkYears();
        $model = $this->model->getAdminShow($id);
        return view($this->view.'edit',array(
            'welfares' => $welfares,
            'rests' => $rests,
            'coms' => $coms,
            'edus' => $edus,
            'workYears' => $workYears,
            'data' => $model,
        ));
    }

    public function update(Request $request,$id)
    {
        $model = $this->model->getAdminShow($id);
        if (!$model) {
            echo "<script>";
            echo "alert('没有记录！');";
            echo "history.go(-1);";
            echo "</script>";
            exit;
        }
        $resData = $this->getData($request);
        $lastId = $this->model->setModify($resData,$id);
        return redirect($this->prefix_url);
    }

    /**
     * 收集、验证
     */
    public function getData(Request $request)
    {
        $res = $request->all();
        if (!$res['cid'] || !$res['job'] || !$res['num'] ||
            !$res['emolument'] || !$res['welfare'] || !$res['rest'] ||
            !$res['sex'] || !$res['edu'] || !$res['num']) {
        }
        $ageStart = $res['ageStart'] ? $res['ageStart'] : 0;
        $ageEnd = $res['ageEnd'] ? $res['ageEnd'] : 0;
        $ageRange = $ageStart.','.$ageEnd;
        $address = $res['address'] ? $res['address'] : '';
        $workHour = $res['workHour'] ? $res['workHour'] : '';
        $contactName = $res['contactName'] ? $res['contactName'] : '';
        $telephone = $res['telephone'] ? $res['telephone'] : '';
        $email = $res['email'] ? $res['email'] : '';
        $skill = $res['skill'] ? $res['skill'] : '';
        return array(
            'cid' => $res['cid'],
            'job' => $res['job'],
            'num' => $res['num'],
            'emolument' => $res['emolument'],
            'welfare' => $res['welfare'],
            'rest' => $res['rest'],
            'sex' => $res['sex'],
            'education' => $res['edu'],
            'work_year' => $res['workYear'],
            'address' => $address,
            'work_hour' => $workHour,
            'contact_name' => $contactName,
            'contact_telephone' => $telephone,
            'email' => $email,
            'age_range' => $ageRange,
            'skill' => $skill,
        );
    }

    /**
     * 获取岗位列表
     */
    public function getJobsByUid(Request $request)
    {
        if (!$request->ajax()) {
            return array('code'=>-1, 'msg'=>'操作失败！');
        }
        $uid = $request->uid;
        $company = $this->model->getComByUid($uid);
        if (!$company) {
            return array('code'=>-2, 'msg'=>'获取企业失败！');
        }
        $cateId = $company->cate==1?1:2;
        $resData = $this->model->getJobs($cateId);
        if (!$resData) {
            return array('code'=>-3, 'msg'=>'没有岗位数据！');
        }
        return array('code'=>0, 'msg'=>'操作成功！', 'data'=>$resData);
    }

    /**
     * 设置删除
     */
    public function setDel(Request $request)
    {
        $id = $request->id;
        $this->model->setDel($id);
        return redirect($this->prefix_url);
    }
}
